package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    private CheckItemService checkItemService;
    @Override
    public Result findPage(QueryPageBean queryPageBean) {

        Page<CheckGroup> page=null;
        QueryWrapper<CheckGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (queryPageBean.getQueryString() == null) {
               page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);

        }else {
            wrapper.like("helpCode",queryPageBean.getQueryString());
            page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);

        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        Result result = new Result(pageResult);
        return result;
    }

    @Override
    @LcnTransaction
    public Boolean add(CheckGroupDTO checkGroupDTO) {
        Integer id = checkGroupDTO.getId();
        if (StringUtils.isNotBlank(String.valueOf(id))) {
            //根据id删除中间表数据
            baseMapper.deleteCheckItemInfoByGroupId(id);
        }

        Boolean flag = this.saveOrUpdate(checkGroupDTO);
        Integer gid = checkGroupDTO.getId();
        //录入中间表
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();
        if (checkitemIds != null && checkitemIds.length > 0) {
            //中间表循环录入
            for (Integer checkitemId : checkitemIds) {
                baseMapper.addCheckGroupAndCheckItemId(gid,checkitemId);
            }
        }


        return flag;
    }

    @Override
    public CheckGroupVO findCheckItemIdsbyGroupId(int id) {

        List<CheckItem> list = checkItemService.list();
        int[] checkitemIds= baseMapper.findCheckItemIdsbyGroupId(id);
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckitemIds(checkitemIds);
        checkGroupVO.setCheckItemList(list);

        return checkGroupVO;
    }

    @Override
    @LcnTransaction
    public Boolean deleteById(int id) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setIs_delete(1);
        boolean flag = updateById(checkGroup);
        return flag;
    }
}
